rm(list=ls())

# Data --------------------------------------------------------------------

country_data <- readRDS("vdem_v13_subset.rds")
party_data <- readRDS("party_time_series.rds")
party_data_reduced <- readRDS("party_time_series_reduced.rds")
stock_data <- readRDS("democratic_stock.rds") %>%
  rename(democratic_stock = s99a_v2x_polyarchy)
opinion_data <- readRDS("claassen_mod5_mean_estimates.rds") %>% 
  mutate(country_name = case_when(country_name == "United States" ~ "United States of America",
                                  country_name == "Bosnia & Herzegovina" ~ "Bosnia and Herzegovina",
                                  country_name == "Côte d’Ivoire" ~ "Ivory Coast",
                                  country_name == "São Tomé & Príncipe" ~ "Sao Tome and Principe",
                                  country_name == "Myanmar (Burma)" ~ "Burma/Myanmar",
                                  country_name == "Trinidad & Tobago" ~ "Trinidad and Tobago",
                                  TRUE ~ country_name))

# Prepare country data ----------------------------------------------------

country_data %>% 
  select(country_name,year,v2x_regime,e_regiongeo,v2x_libdem,v2x_polyarchy,e_gdppc,e_pelifeex,v2cacamps) %>%
  filter(year%in% c(1990:2019))  %>% 
  rename(regime_type = v2x_regime,
         region = e_regiongeo,
         liberal_democracy = v2x_libdem,
         polyarchy = v2x_polyarchy,
         GDP_capita = e_gdppc,
         life_expectancy = e_pelifeex,
         polarization = v2cacamps)  %>% 
  mutate(regime_type = case_when(regime_type == 0 ~ "Closed Autocracy",
                                 regime_type == 1 ~ "Electoral Autocracy",
                                 regime_type == 2 ~ "Electoral Democracy",
                                 regime_type == 3 ~ "Liberal Democracy")) ->
  country_data_recoded

# FULL DATA ---------------------------------------------------------------

# Merge datasets ----------------------------------------------------------

country_data_recoded %>% 
  left_join(party_data, by = c("country_name" = "country","year")) %>% 
  left_join(stock_data, by = c("country_name","year")) %>% 
  left_join(opinion_data, by = c("country_name","year")) -> 
  merged_data

# Region means ------------------------------------------------------------

merged_data %>% 
  group_by(region,year) %>%
  summarise(region_liberal_democracy = mean(liberal_democracy, na.rm = TRUE),
            region_polyarchy = mean(polyarchy, na.rm = TRUE),
            region_antipluralism_gov_seat_share = mean(antipluralism_gov_seat_share, na.rm = TRUE),
            region_antipluralism_gov_vote_share = mean(antipluralism_gov_vote_share, na.rm = TRUE),
            region_antipluralism_gov_head_seat_share = mean(antipluralism_gov_head_seat_share, na.rm = TRUE),
            region_citizen_support = mean(citizen_support, na.rm = TRUE))  %>%
  mutate(across(c(region_liberal_democracy,region_polyarchy,region_antipluralism_gov_seat_share,region_antipluralism_gov_vote_share,region_citizen_support),
                ~ ifelse(. == "NaN", NA_real_, .))) ->
  region_data

# Merge region to data ----------------------------------------------------

merged_data %>%
  left_join(region_data, by = c("region","year")) ->
  merged_data2

# Identify democracies ----------------------------------------------------

merged_data2 %>% 
  filter(regime_type %in% c("Electoral Democracy","Liberal Democracy")) %>%
  pull(country_name) -> 
  democracies

list_democracies <- levels(as.factor(democracies))

# Filter for democracies --------------------------------------------------

merged_data2 %>%
  filter(country_name %in% list_democracies) %>%
  drop_na(citizen_support,antipluralism_gov_seat_share) ->
  democracy_data

# Remove pre-democracy observations ---------------------------------------

democracy_data  %>% 
  filter(regime_type %in% c("Electoral Democracy","Liberal Democracy")) %>% 
  group_by(country_name) %>% 
  summarise(first_democracy = first(year)) ->
  first_democracy

democracy_data %>%  
  left_join(first_democracy, by = c("country_name")) %>%
  mutate(prior_democracy = case_when(first_democracy > year ~ 1,
                                     first_democracy <= year ~ 0)) %>%  
  filter(prior_democracy != 1,
         country_name != "Armenia") %>%
  mutate(country_name = droplevels(as.factor(country_name))) ->
  democracy_data2

# Standardize variables by two SDs ----------------------------------------

democracy_data2  %>% 
  mutate(liberal_democracy = liberal_democracy*100,
         polyarchy = polyarchy*100,
         citizen_support = effectsize::standardize(citizen_support,two_sd = T),
         antipluralism_gov_seat_share = effectsize::standardize(antipluralism_gov_seat_share,two_sd = T),
         antipluralism_gov_vote_share = effectsize::standardize(antipluralism_gov_vote_share,two_sd = T),
         antipluralism_gov_head_seat_share = effectsize::standardize(antipluralism_gov_head_seat_share,two_sd = T),
         GDP_capita = as.numeric(effectsize::standardize(log(GDP_capita),two_sd = T)),
         life_expectancy = effectsize::standardize(life_expectancy,two_sd = T),
         polarization = effectsize::standardize(polarization,two_sd = T),
         clientelism_gov_seat_share = effectsize::standardize(clientelism_gov_seat_share,two_sd = T),
         democratic_stock = effectsize::standardize(democratic_stock,two_sd = T),
         region_liberal_democracy = effectsize::standardize(region_liberal_democracy,two_sd = T),
         region_polyarchy = effectsize::standardize(region_polyarchy,two_sd = T),
         region_antipluralism_gov_seat_share = effectsize::standardize(region_antipluralism_gov_seat_share,two_sd = T),
         region_antipluralism_gov_vote_share = effectsize::standardize(region_antipluralism_gov_vote_share,two_sd = T),
         region_antipluralism_gov_head_seat_share = effectsize::standardize(region_antipluralism_gov_head_seat_share,two_sd = T),
         region_citizen_support = effectsize::standardize(region_citizen_support,two_sd = T)) -> 
  full_tscs_data

panel <- pdata.frame(full_tscs_data, index = c("country_name", "year"), row.names = FALSE)

# create lags for interaction DVs
panel$antipluralism_gov_seat_share_lag <- lag(panel$antipluralism_gov_seat_share,1)
panel$antipluralism_gov_vote_share_lag <- lag(panel$antipluralism_gov_vote_share,1)
panel$antipluralism_gov_head_seat_share_lag <- lag(panel$antipluralism_gov_head_seat_share,1)
panel$citizen_support_lag <- lag(panel$citizen_support,1)

# create lags for interaction DVs 2 year lag
panel$antipluralism_gov_seat_share_lag2 <- lag(panel$antipluralism_gov_seat_share,2)
panel$citizen_support_lag2 <- lag(panel$citizen_support,2)

# Liberal democracy downturn ----------------------------------------------

# create downturn variable
panel$diff = diff(panel$liberal_democracy, 1)
panel$downturn = ifelse(panel$diff>0,0,panel$diff)*-1

# Electoral democracy downturn --------------------------------------------

# create downturn variable
panel$diff_polyarchy = diff(panel$polyarchy, 1)
panel$downturn_polyarchy = ifelse(panel$diff_polyarchy>0,0,panel$diff_polyarchy)*-1

# Create MCMC draws data --------------------------------------------------

mcmc_draws <- readRDS("claassen_mod5_draw_estimates.rds")

no <- paste("X",1:1000,sep = "")

rescale_variable <- function(x) as.numeric(effectsize::standardize(lag(paste(x),1),two_sd = T))

mcmc_draws %>% mutate_at(no,rescale_variable) -> 
  mcmc_rescaled

full_tscs_data  %>% 
  left_join(mcmc_rescaled, by = c("country_name","year")) ->
  panel_draws

panel_draws <- pdata.frame(panel_draws, index = c("country_name", "year"), row.names = FALSE)

# create downturn variable
panel_draws$diff = diff(panel_draws$liberal_democracy, 1)
panel_draws$downturn = ifelse(panel_draws$diff>0,0,panel_draws$diff)*-1

# Electoral democracy downturn --------------------------------------------

# create downturn variable
panel_draws$diff_polyarchy = diff(panel_draws$polyarchy, 1)
panel_draws$downturn_polyarchy = ifelse(panel_draws$diff_polyarchy>0,0,panel_draws$diff_polyarchy)*-1

# Save --------------------------------------------------------------------

save(panel, file = "TSCS_data.RData")
save(panel_draws, file = "TSCS_data_draws.RData")

# REDUCED DATA ------------------------------------------------------------

# Merge datasets ----------------------------------------------------------

country_data_recoded %>% 
  left_join(party_data_reduced, by = c("country_name" = "country","year")) %>% 
  left_join(stock_data, by = c("country_name","year")) %>% 
  left_join(opinion_data, by = c("country_name","year")) -> 
  merged_data

# Region means ------------------------------------------------------------

merged_data %>% 
  group_by(region,year) %>%
  summarise(region_liberal_democracy = mean(liberal_democracy, na.rm = TRUE),
            region_polyarchy = mean(polyarchy, na.rm = TRUE),
            region_antipluralism_gov_seat_share = mean(antipluralism_gov_seat_share, na.rm = TRUE),
            region_antipluralism_gov_vote_share = mean(antipluralism_gov_vote_share, na.rm = TRUE),
            region_antipluralism_gov_head_seat_share = mean(antipluralism_gov_head_seat_share, na.rm = TRUE),
            region_citizen_support = mean(citizen_support, na.rm = TRUE))  %>%
  mutate(across(c(region_liberal_democracy,region_polyarchy,region_antipluralism_gov_seat_share,region_antipluralism_gov_vote_share,region_citizen_support),
                ~ ifelse(. == "NaN", NA_real_, .))) ->
  region_data

# Merge region to data ----------------------------------------------------

merged_data %>%
  left_join(region_data, by = c("region","year")) ->
  merged_data2

# Identify democracies ----------------------------------------------------

merged_data2 %>% 
  filter(regime_type %in% c("Electoral Democracy","Liberal Democracy")) %>%
  pull(country_name) -> 
  democracies

list_democracies <- levels(as.factor(democracies))

# Filter for democracies --------------------------------------------------

merged_data2 %>%
  filter(country_name %in% list_democracies) %>%
  drop_na(citizen_support,antipluralism_gov_seat_share) ->
  democracy_data

# Remove pre-democracy observations ---------------------------------------

democracy_data  %>% 
  filter(regime_type %in% c("Electoral Democracy","Liberal Democracy")) %>% 
  group_by(country_name) %>% 
  summarise(first_democracy = first(year)) ->
  first_democracy

democracy_data %>%  
  left_join(first_democracy, by = c("country_name")) %>%
  mutate(prior_democracy = case_when(first_democracy > year ~ 1,
                                     first_democracy <= year ~ 0)) %>%  
  filter(prior_democracy != 1,
         country_name != "Armenia") %>%
  mutate(country_name = droplevels(as.factor(country_name))) ->
  democracy_data2

# Standardize variables by two SDs ----------------------------------------

democracy_data2  %>% 
  mutate(liberal_democracy = liberal_democracy*100,
         polyarchy = polyarchy*100,
         citizen_support = effectsize::standardize(citizen_support,two_sd = T),
         antipluralism_gov_seat_share = effectsize::standardize(antipluralism_gov_seat_share,two_sd = T),
         antipluralism_gov_vote_share = effectsize::standardize(antipluralism_gov_vote_share,two_sd = T),
         antipluralism_gov_head_seat_share = effectsize::standardize(antipluralism_gov_head_seat_share,two_sd = T),
         GDP_capita = as.numeric(effectsize::standardize(log(GDP_capita),two_sd = T)),
         life_expectancy = effectsize::standardize(life_expectancy,two_sd = T),
         polarization = effectsize::standardize(polarization,two_sd = T),
         clientelism_gov_seat_share = effectsize::standardize(clientelism_gov_seat_share,two_sd = T),
         democratic_stock = effectsize::standardize(democratic_stock,two_sd = T),
         region_liberal_democracy = effectsize::standardize(region_liberal_democracy,two_sd = T),
         region_polyarchy = effectsize::standardize(region_polyarchy,two_sd = T),
         region_antipluralism_gov_seat_share = effectsize::standardize(region_antipluralism_gov_seat_share,two_sd = T),
         region_antipluralism_gov_vote_share = effectsize::standardize(region_antipluralism_gov_vote_share,two_sd = T),
         region_antipluralism_gov_head_seat_share = effectsize::standardize(region_antipluralism_gov_head_seat_share,two_sd = T),
         region_citizen_support = effectsize::standardize(region_citizen_support,two_sd = T)) -> 
  full_tscs_data

panel <- pdata.frame(full_tscs_data, index = c("country_name", "year"), row.names = FALSE)

# create lags for interaction DVs
panel$antipluralism_gov_seat_share_lag <- lag(panel$antipluralism_gov_seat_share,1)
panel$antipluralism_gov_vote_share_lag <- lag(panel$antipluralism_gov_vote_share,1)
panel$antipluralism_gov_head_seat_share_lag <- lag(panel$antipluralism_gov_head_seat_share,1)
panel$citizen_support_lag <- lag(panel$citizen_support,1)

# create lags for interaction DVs 2 year lag
panel$antipluralism_gov_seat_share_lag2 <- lag(panel$antipluralism_gov_seat_share,2)
panel$citizen_support_lag2 <- lag(panel$citizen_support,2)

# Liberal democracy downturn ----------------------------------------------

# create downturn variable
panel$diff = diff(panel$liberal_democracy, 1)
panel$downturn = ifelse(panel$diff>0,0,panel$diff)*-1

# Electoral democracy downturn --------------------------------------------

# create downturn variable
panel$diff_polyarchy = diff(panel$polyarchy, 1)
panel$downturn_polyarchy = ifelse(panel$diff_polyarchy>0,0,panel$diff_polyarchy)*-1

# Create MCMC draws data --------------------------------------------------

mcmc_draws <- readRDS("claassen_mod5_draw_estimates.rds")

no <- paste("X",1:1000,sep = "")

rescale_variable <- function(x) as.numeric(effectsize::standardize(lag(paste(x),1),two_sd = T))

mcmc_draws %>% mutate_at(no,rescale_variable) -> 
  mcmc_rescaled

full_tscs_data  %>% 
  left_join(mcmc_rescaled, by = c("country_name","year")) ->
  panel_draws

panel_draws <- pdata.frame(panel_draws, index = c("country_name", "year"), row.names = FALSE)

# create downturn variable
panel_draws$diff = diff(panel_draws$liberal_democracy, 1)
panel_draws$downturn = ifelse(panel_draws$diff>0,0,panel_draws$diff)*-1

# Electoral democracy downturn --------------------------------------------

# create downturn variable
panel_draws$diff_polyarchy = diff(panel_draws$polyarchy, 1)
panel_draws$downturn_polyarchy = ifelse(panel_draws$diff_polyarchy>0,0,panel_draws$diff_polyarchy)*-1

# Save --------------------------------------------------------------------

save(panel, file = "TSCS_data_reduced.RData")